package com.itmg.tools.thread;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itmg.util.StringUtil;

/**
 * shop搜索历史的线程类
 * @author Jason
 *
 */
public class ShopHistoryThread implements Runnable{
	
	private HttpServletRequest request;
	private HttpServletResponse response;
	private List<String> historyURLList = new ArrayList<String>();
	
    public ShopHistoryThread(HttpServletRequest request,HttpServletResponse response){
    	this.request = request;
    	this.response = response;
    }
    
	public void run() {
		try{
			 String keywords = request.getParameter("keywords");
//			 String category = request.getParameter("category");
//			 String isAuction = request.getParameter("isAuction");
//			 String sort = request.getParameter("sort");
			 
			 Cookie[] cookies = request.getCookies();
			 boolean flag  = false;
			 List<Cookie> cookieList = new ArrayList<Cookie>(); 
			 if(cookies!=null){
				 for(Cookie c : cookies){
					 if(c.getName().indexOf("coojin_shop_history")!=-1){
						 String value = c.getValue();
						 if(!StringUtil.checkIsEmpty(value)){
							 historyURLList.add(value);
						 }
						 flag = true;
						 cookieList.add(c);
					 }
				 }
			 }
			 String URL = "";
//			 if(isAuction != null && !"0".equals(isAuction)){
//	 			 URL = "<a href='"+request.getContextPath()+"/word/"+URLEncoder.encode(keywords, "utf-8")+"/"+0+"/"+URLEncoder.encode(sort,"utf-8")+"/"+URLEncoder.encode(isAuction,"utf-8")+"'>"+URLEncoder.encode(keywords, "utf-8")+"</a>";
//	 		 }else{
	 			 URL = "<a href='"+request.getContextPath()+"/word/"+URLEncoder.encode(keywords, "utf-8")+"/"+"'>"+URLEncoder.encode(keywords, "utf-8")+"</a>";
//	 		 }
			 URL = URL.replaceAll("(\r|\n)", "");
			 if(!flag){
				 Cookie cookie1 = new Cookie("coojin_shop_history_1", URL);
				 Cookie cookie2 = new Cookie("coojin_shop_history_2","");
				 Cookie cookie3 = new Cookie("coojin_shop_history_3", "");
				 Cookie cookie4 = new Cookie("coojin_shop_history_4", "");
				 cookie1.setMaxAge(365*24*60*60);
				 cookie2.setMaxAge(365*24*60*60);
				 cookie3.setMaxAge(365*24*60*60);
				 cookie4.setMaxAge(365*24*60*60);
			     cookie1.setPath("/");
			     cookie2.setPath("/");
			     cookie3.setPath("/");
			     cookie4.setPath("/");
			     response.addCookie(cookie1);
			     response.addCookie(cookie2);
			     response.addCookie(cookie3);
			     response.addCookie(cookie4);
			 }else{
				 if(!historyURLList.contains(URL)){
					 Cookie cookie = cookieList.get(1);
					 cookie.setMaxAge(0);
					 cookie.setPath("/");
					 response.addCookie(cookie);
					 
					 cookie = new Cookie(cookie.getName(),URL);
					 cookie.setMaxAge(365*24*60*60);
					 cookie.setPath("/");
					 response.addCookie(cookie);
				 }
				 List<String> endList = new ArrayList<String>();
				 for(String value : historyURLList){
					 endList.add(URLDecoder.decode(value, "UTF-8"));
				 }
				 historyURLList = endList;
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public List<String> getHistoryURLList() {
		return historyURLList;
	}

}
